package com.xhh.reapply.listener;

import com.xhh.common.constants.ReapplyConstants;
import com.xhh.model.reapply.pojos.Reapply;
import com.xhh.reapply.service.ReapplyService;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class ScanSuccessListener {

    private final ReapplyService reapplyService;

    @RabbitListener(bindings = @QueueBinding(
        value = @Queue(value = ReapplyConstants.QUEUE_NAME, durable = "true"),
        exchange = @Exchange(ReapplyConstants.EXCHANGE_NAME),
        key = ReapplyConstants.SCAN_SUCCESS_KEY
    ))
    public void listenScanSuccess(Long reapplyId){
        // 1. 获取调剂志愿
        Reapply reapply = reapplyService.getById(reapplyId);
        // 2.幂等判断，判断状态是否为701
        if (reapply == null || !reapply.getStatus().equals(ReapplyConstants.ADD_SUCCESS_CHOICE_STATUS)) {
            return;
        }
        // 3.标记状态为已被招生单位查看
        reapply.setStatus(ReapplyConstants.SCANED_CHOICE_STATUS);
        reapplyService.updateById(reapply);
    }
}
